# Prepare 2020 national returns
# By Dom Valentino and Chris Kenny
# libs --------------------------------------------------------------------
library(plyr) # rbind.fill()
library(tidyverse) # select()
library(openintro) # state2abbr()

# set wd to source file location
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))

# data --------------------------------------------------------------------
us <- readRDS("../data/politico_2020/elec2020.Rds")
mt <- read.csv("../data/analysis/analysis.csv") %>% 
  filter(state == "MT", year == 2020) %>% 
  select(state, county, year, treat, dem_share_pres, dem_share_gov, dem_share_sen)

temp <- us %>% select("state" = "STATE", "county" = "COUNTY", "year", "office" = "race", "d_votes" = "D VOTES", "r_votes" = "R VOTES") %>% 
  mutate(county = str_replace(county, " County| Parish| Borough", ""), 
         state = state2abbr(state),
         dem_share = d_votes / (d_votes + r_votes),
         treat = case_when(
           state %in% c("TX", "LA", "MS", "TN", "IN") ~ 0,
           state %in% c("ID", "WY", "ND", "SD", "NM", "KS", "OK", "MO", "AR", "KY", "AL", "AK", "GA", "FL", "SC", "NC", "WA", "PA", "WV", "VA", "MI", "NY", "NH", "ME") ~ 0,
           state %in% c("AZ", "NE", "MN", "IA", "WI", "IL", "OH", "MA", "RI", "CT", "DE", "MD") ~ 0,
           state %in% c("WA", "OR", "CA", "NV", "UT", "CO", "NJ", "VT", "HI") ~ 1),
         office = case_when(
           office == "governor" ~ "dem_share_gov",
           office == "president" ~ "dem_share_pres",
           office == "senate" ~ "dem_share_sen")) %>%
  mutate(county = str_replace(county, " City| city", "")) %>% 
  filter(!(state %in% c("MT"))) %>% 
  select(-d_votes, -r_votes) %>% 
  pivot_wider(names_from = "office", values_from = "dem_share")
  
# bind montana (easier this way because i already coded county-level treatment)
final <- rbind.fill(temp, mt) %>% 
  mutate(county_id = NA)

sum(duplicated(final))

write_csv(final, "../data/analysis/analysis2020.csv")
